package com.fh.fruit.service.pic.impl;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.fh.dao.DaoSupport;
import com.fh.fruit.service.goods.GoodsManager;
import com.fh.fruit.service.pic.FileManager;
import com.fh.fruit.util.FtpUtil;


/**
 * 文件上传服务
 * 
 * @author Administrator
 *
 */
@Service
public class FileSevice implements FileManager {
	
	@Value("${FTP_ADDRESS}")
	private String FTP_ADDRESS;
	@Value("${FTP_PORT}")
	private Integer FTP_PORT;
	@Value("${FTP_USERNAME}")
	private String FTP_USERNAME;
	@Value("${FTP_PASSWORD}")
	private String FTP_PASSWORD;
	@Value("${FTP_BASE_PATH}")
	private String FTP_BASE_PATH;
	@Value("${FILE_BASE_URL}")
	private String FILE_BASE_URL;
	
	@Resource(name="goodsService")
	private GoodsManager goodsService;
	@Resource(name = "daoSupport")
	private DaoSupport dao;



	@Override
	public Map<String, Object> uploadFile(InputStream uploadFile, String path,String name) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {

			boolean result = FtpUtil.uploadFile(FTP_ADDRESS, FTP_PORT, FTP_USERNAME, FTP_PASSWORD, FTP_BASE_PATH,
					path, name, uploadFile);
			// 返回结果
			if (!result) {
				resultMap.put("error", 1);
				resultMap.put("message", "文件上传失败");
				return resultMap;
			}
			resultMap.put("error", 0);
			resultMap.put("url", FILE_BASE_URL + "/" + path + "/" + name);
			return resultMap;

		} catch (Exception e) {
			resultMap.put("error", 1);
			resultMap.put("message", "文件上传发生异常");
			return resultMap;
		}
	}
	
	
	@Override
	public Map<String, Object> uploadFile(MultipartFile uploadFile,String path,String name) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			boolean result = FtpUtil.uploadFile(FTP_ADDRESS, FTP_PORT, FTP_USERNAME, FTP_PASSWORD, FTP_BASE_PATH,
					path, name, uploadFile.getInputStream());
			// 返回结果
			if (!result) {
				resultMap.put("error", 1);
				resultMap.put("message", "文件上传失败");
				return resultMap;
			}
			resultMap.put("error", 0);
			resultMap.put("url", FILE_BASE_URL + "/" + path + "/" + name);
			return resultMap;

		} catch (Exception e) {
			resultMap.put("error", 1);
			resultMap.put("message", "文件上传发生异常");
			return resultMap;
		}
	}
	


}
